﻿using UnityEngine;
using System;
using System.Collections.Generic;

namespace Room
{
    public class RCAIUnionSelf : RCAIUnionActor
    {
        private bool pressing = false;
        private bool stoped = true;
        private Vector3 moveDirection = Vector3.zero;

        public int killNum;
        private bool autoPathFinding;
        public bool AutoPathFinding
        {
            get
            {
                return autoPathFinding;
            }
        }
        public RoomCharactor autoClickCharactor;
        private Action arrivedCallback;
        
        public RCAIUnionSelf(RoomCharactor owner)
            : base(owner)
        {
            type = AIType.UnionSelf;
        }

        public override void SetKillNum(int killNum)
        {
            this.killNum = killNum;
            var data = Dict.Blo.DictKillerBlo.GetKillerData(killNum);
            var color = new Color(data.colorR / 255f, data.colorG / 255f, data.colorB / 255f);
            if (color.Equals(Color.white))
            {
                owner.ui.SetColor(new Color((float)0x79 / (float)0xff, (float)0xed / (float)0xff, (float)0x1c / (float)0xff));
            }
            else
            {
                owner.ui.SetColor(color);
            }
        }

        public override int GetKillNum()
        {
            return killNum;
        }

        public override void Tick()
        {
            base.Tick();

            pressing = pressing && TouchKit.GetEnabled();
            if (pressing && !(moveDirection.x == 0f && moveDirection.y == 0f))
            {
                Vector3 to = owner.transform.position;
                to += moveDirection;
                
                owner.MoveTo(to);
                
                autoPathFinding = false;
                autoClickCharactor = null;
                arrivedCallback = null;

                if (autoClickCharactor != null)
                {
                    Debug.Log("self move to " + to + " id " + autoClickCharactor.name);
                }

                stoped = false;
                owner.ai.SwitchState((int)UnionActorState.Move);
            }
            else
            {
                if (!stoped)
                {
                    Vector3 to = owner.transform.position;
                    to += moveDirection;

                    owner.StopTo(owner.transform.position);
                    stoped = true;
                }
            }
            if (autoPathFinding)
            {
                if (!owner.IsMoving())
                {
                    if (autoClickCharactor != null)
                    {
                        autoClickCharactor.Click();
                    }

                    if (arrivedCallback != null)
                    {
                        arrivedCallback();
                    }

                    autoPathFinding = false;
                    arrivedCallback = null;
                    autoClickCharactor = null;
                }
            }


            if (!isShow)
                return;
            if (showTime > 0)
            {
                showTime -= Time.deltaTime;
            }
            else
            {
                isShow = false;
                CreateAchieveEffect();
            }
        }
        int i = 0;
        public override void DoResume()
        {
            TouchKit.On_MoveStart = this.OnJoystickMoveStart;
            TouchKit.On_MoveEnd = this.OnJoystickMoveEnd;
            TouchKit.On_Move = this.OnJoystickMove;

            owner.charactorManager.room.uiManager.onCharactorClickCallback += OnAnyCharactorClick;
            
            var heroId = StaticData.playerData.GetBattleCamp(1).hero.heroId;

            string weaponId = "";
            if (StaticData.playerData.GetBattleCamp(1).equipments[0] != null)
                weaponId = StaticData.playerData.GetBattleCamp(1).equipments[0].equipId;


            var quest = Story.StoryManager.Instance.GetCurQuest();
            //当前有正在进行的任务且任务要求地图里跑路的是主角
            if (quest != null && quest.forceMain == 1)
            {
                var uiHero = StaticData.playerData.GetUIHero(StaticData.playerData.heroWarUID);
                heroId = uiHero.heroId;
                for (int i = 1; i < 5; i++)
                {
                    var campHero = StaticData.playerData.GetBattleCamp(i).hero;
                    if (campHero != null && campHero.guid.Equals(StaticData.playerData.heroWarUID))
                    {
                        var campWeapon = StaticData.playerData.GetBattleCamp(i).equipments[0];
                        if (campWeapon != null)
                            weaponId = campWeapon.equipId;
                    }
                }
            }

            RCLODPlayer lod = owner.lod as RCLODPlayer;
            lod.Refresh(heroId, weaponId);

            RCUIPlayer ui = owner.ui as RCUIPlayer;
            if (ui != null)
            {
                ui.UpdateNamePlace();
            }
            ui.SetTitle(StaticData.playerData.curTitle);
            //'ui.SetUinonServer(StaticData.playerData.un, StaticData.playerData.serverUID);
        }
        public override void DoPause()
        {
            TouchKit.On_MoveStart = null;
            TouchKit.On_MoveEnd = null;
            TouchKit.On_Move = null;

            owner.charactorManager.room.uiManager.onCharactorClickCallback -= OnAnyCharactorClick;

            stoped = true;
            pressing = false;
            StopAutoGo();
        }
        private void OnJoystickMove(Vector2 joystickAxis)
        {
            if (UI.UnionFightData.IsOnHook)
            {
                var str = TableTextBlo.GetText("union_fight_on_hook_tip");
                BoxManager.CreateMessageBoxWithCofirmAndCancelButton(str, (b) =>
                {
                    if (b)
                    {
                        var net = NetUnionFightBase.GetNet();
                        if (net != null)
                        {
                            net.SendCSUFOnHook(false);
                        }
                    }
                });
                return;
            }

            moveDirection = new Vector3(joystickAxis.x, 0, joystickAxis.y).normalized * 0.3f;
            Quaternion que = Quaternion.Euler(StaticData.cameraRotate);
            moveDirection = que * moveDirection;
        }
        private void OnJoystickMoveStart()
        {
            

            pressing = true;
        }
        private void OnJoystickMoveEnd()
        {
            pressing = false;
            moveDirection = Vector3.zero;
        }

        private void OnAnyCharactorClick(RoomCharactor charactor)
        {
            
        }

        public void GoToAndClick(RoomCharactor charactor, float range = 0.5f)
        {
            Vector3 target = Vector3.MoveTowards(charactor.transform.position, owner.transform.position, range);

            owner.StopTo(target);
            autoPathFinding = true;
            autoClickCharactor = charactor;
            arrivedCallback = null;
        }

        public void AutoGoto(Vector3 target, Action callback, float range = 0.8f)
        {
            Vector3 target0 = Vector3.MoveTowards(target, owner.transform.position, range);
            owner.StopTo(target0);
            autoPathFinding = true;
            arrivedCallback = callback;
            autoClickCharactor = null;
        }

        public void StopAutoGo()
        {
            owner.StopMove();
            autoPathFinding = false;
            autoClickCharactor = null;
            arrivedCallback = null;
        }


        GameObject effect;
        Queue<string> achieveQueue = new Queue<string>();
        float showTime;
        bool isShow;
        public void AchieveHorner(int type)
        {
            string str = "";
            switch (type)
            {
                case 0:
                    str = RoomUnionFight.Five_Win;
                    break;
                case 1:
                    str = RoomUnionFight.Ten_Win;
                    break;
                case 2:
                    str = RoomUnionFight.Revenge;
                    break;
                case 3:
                    str = RoomUnionFight.Gate_Destroy;
                    break;
                default:
                    Debug.LogError("type Error: " + type);
                    break;
            }
            achieveQueue.Enqueue(str);
            CreateAchieveEffect();
        }

        void CreateAchieveEffect()
        {
            if (isShow || achieveQueue.Count <= 0)
                return;
            else
            {
                string str = achieveQueue.Dequeue();
                showTime = 1.5f;
                effect = SpawnOnUI(str, showTime);
                var renders = effect.GetComponentsInChildren<MeshRenderer>(true);
                foreach (var render in renders)
                {
                    render.sortingOrder++;
                }
                effect.transform.localPosition = new Vector3(0, 5, 0);
                isShow = true;
            }
        }

        public void CreateCountDown()
        {
            GameObject countDown = SpawnOnUI(RoomUnionFight.Count_Down, 6f);
            var renders = countDown.GetComponentsInChildren<MeshRenderer>(true);
            foreach(var render in renders)
            {
                render.sortingOrder++;
            }
        }
    }

}